package controller

import (
	"sale-admin/config/mysql"
	"sale-admin/internal/app/web/service/order"

	"192.168.1.75/go-pkg/errorx"
	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type Order struct{}

// List 订单列表
func (o Order) List(c *gin.Context) {
	var params struct {
		Page        int       `json:"page"`
		Limit       int       `json:"limit"`
		SalesID     string    `json:"sales_id"`
		ShopID      *[]string `json:"shop_id"`
		PayStatus   int       `json:"pay_status"`
		OrderStatus string    `json:"order_status"`
		RefundType  *int      `json:"refund_type"`
		GoodsName   string    `json:"goods_name"`
		ClassID     string    `json:"class_id"`
		StartTime   string    `json:"start_time"`
		EndTime     string    `json:"end_time"`
		PaymentID   string    `json:"payment_id"`
		OutTradeNo  string    `json:"out_trade_no"`
		Areas1      string    `json:"areas1"`
		Areas2      string    `json:"areas2"`
		Areas3      string    `json:"areas3"`
		Areas5      string    `json:"areas5"`
	}
	err := c.ShouldBindBodyWith(&params, binding.JSON)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}

	merchantID := c.GetString("merchant_id")
	userID := c.GetString("user_id")
	// 获取商户列表
	var shopIDList = []string{}
	if params.ShopID != nil {
		shopIDList = append(shopIDList, *params.ShopID...)
	}
	db := mysql.NewDB()
	data, count, _, err := order.OrderService{}.OrderList(
		c.Request.Context(), db, merchantID, userID, params.Page, params.Limit, params.SalesID, shopIDList, params.PayStatus,
		params.OrderStatus, params.RefundType, params.GoodsName, params.ClassID, params.StartTime, params.EndTime, params.PaymentID, params.OutTradeNo,
		params.Areas1, params.Areas2, params.Areas3, params.Areas5, false)
	response.JSON(c, gin.H{
		"data":  data,
		"count": count,
	}, err)
}

// RefundAnalyze 实现每个商户的退款数量、退款金额、退款原因的分析（共计5个检索条件）
func (o Order) RefundAnalyze(c *gin.Context) {
	var params struct {
		// 分页搜索
		Page  int `json:"page"`
		Limit int `json:"limit"`
		// 1.检索设备
		SalesID string `json:"sales_id"`
		// 2.检索日期
		StartTime string `json:"start_time"`
		EndTime   string `json:"end_time"`
		// 3.检索归属商户
		ShopID *[]string `json:"shop_id"`
		// 4.检索所在地区
		Areas1 string `json:"areas1"`
		Areas2 string `json:"areas2"`
		// 5.检索所在位置
		Areas3 string `json:"areas3"`
	}
	err := c.ShouldBindBodyWith(&params, binding.JSON)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}
	// 当前登录的商户id
	merchantID := c.GetString("merchant_id")
	userID := c.GetString("user_id")
	// 获取当前登录账号下的所有商户列表
	var shopIDList = []string{}
	if params.ShopID != nil {
		shopIDList = append(shopIDList, *params.ShopID...)
	}
	// 获取数据
	data, err := order.OrderService{}.RefundOrderAnalyze(
		c.Request.Context(),
		merchantID,
		userID,
		params.Page, params.Limit,
		params.SalesID,
		shopIDList,
		params.StartTime,
		params.EndTime,
		params.Areas1,
		params.Areas2,
		params.Areas3,
	)
	returnData := gin.H{
		"data":     data,
		"sales_id": params.SalesID,
	}
	response.JSON(c, returnData, err)
}

// 退款列表
func (o Order) RefundList(c *gin.Context) {
	var params struct {
		Page        int       `json:"page"`
		Limit       int       `json:"limit"`
		SalesID     string    `json:"sales_id"`
		ShopID      *[]string `json:"shop_id"`
		PayStatus   int       `json:"pay_status"`
		OrderStatus string    `json:"order_status"`
		RefundType  *int      `json:"refund_type"` // 筛选退款类型 0 手动退款 1自动退款
		GoodsName   string    `json:"goods_name"`
		ClassID     string    `json:"class_id"`
		StartTime   string    `json:"start_time"`
		EndTime     string    `json:"end_time"`
		PaymentID   string    `json:"payment_id"`
		OutTradeNo  string    `json:"out_trade_no"`
		Areas1      string    `json:"areas1"`
		Areas2      string    `json:"areas2"`
		Areas3      string    `json:"areas3"`
		Areas5      string    `json:"areas5"`
	}
	err := c.ShouldBindBodyWith(&params, binding.JSON)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}

	merchantID := c.GetString("merchant_id")
	userID := c.GetString("user_id")
	var shopIDList = []string{}
	if params.ShopID != nil {
		shopIDList = append(shopIDList, *params.ShopID...)
	}

	// 获取数据
	data, count, totalCount, err := order.OrderService{}.RefundOrderList(c.Request.Context(), merchantID, userID,
		params.Page, params.Limit,
		params.SalesID,
		shopIDList,
		params.PayStatus,
		params.OrderStatus,
		params.RefundType,
		params.GoodsName,
		params.ClassID,
		params.StartTime,
		params.EndTime,
		params.PaymentID,
		params.OutTradeNo,
		params.Areas1,
		params.Areas2,
		params.Areas3,
		params.Areas5,
	)
	returnData := gin.H{
		"data":        data,
		"sales_id":    params.SalesID,
		"count":       count,
		"total_count": totalCount,
	}
	response.JSON(c, returnData, err)
}

// 后台手动退款
func (o Order) Refund(c *gin.Context) {
	var params struct {
		PaymentAmount float64 `json:"payment_amount"`
		OutTradeNo    string  `json:"out_trade_no"`
		Remarks       string  `json:"remarks"`
		Reason        string  `json:"reason"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)

	merchantID := c.GetString("merchant_id")
	userID := c.GetString("user_id")

	db := mysql.NewDB()

	data, err := order.OrderRefund{}.Refund(
		c.Request.Context(),
		db,
		merchantID,
		params.OutTradeNo, // 有子订单的，传子订单。没有传主订单。金额需要通过订单号从数据查询获取，不依赖于前端。
		params.PaymentAmount,
		params.Reason,
		userID,
	)
	response.JSON(c, data, err)
}

// ExportOrderList 导出订单列表
func (o Order) ExportOrderList(c *gin.Context) {
	var p struct {
		Page        int       `json:"page"`
		Limit       int       `json:"limit"`
		SalesID     string    `json:"sales_id"`
		ShopID      *[]string `json:"shop_id"`
		PayStatus   int       `json:"pay_status"`
		OrderStatus string    `json:"order_status"`
		RefundType  *int      `json:"refund_type"`
		GoodsName   string    `json:"goods_name"`
		StartTime   int64     `json:"start_time"`
		EndTime     int64     `json:"end_time"`
		PaymentID   string    `json:"payment_id"`
		OutTradeNo  string    `json:"out_trade_no"`
		Areas1      string    `json:"areas1"`
		Areas2      string    `json:"areas2"`
		Areas3      string    `json:"areas3"`
		OrderID     string    `json:"order_id"`
	}
	err := c.ShouldBindBodyWith(&p, binding.JSON)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}

	merchantID := c.GetString("merchant_id")
	var shopIDList = []string{}
	if p.ShopID != nil {
		shopIDList = append(shopIDList, *p.ShopID...)
	}
	db := mysql.NewDB()
	data, count, totalCount, err := order.OrderService{}.ExportOrderList(
		c.Request.Context(), db, merchantID, p.Limit,
		p.SalesID, shopIDList, p.PayStatus, p.OrderStatus, p.RefundType,
		p.GoodsName, p.StartTime, p.EndTime, p.PaymentID, p.OutTradeNo,
		p.Areas1, p.Areas2, p.Areas3,
		p.OrderID,
	)
	response.JSON(c, gin.H{
		"data":        data,
		"count":       count,
		"total_count": totalCount,
	}, err)
}
